﻿<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="keyword" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchSysUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddSysUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableSysUser" lay-filter="tableSysUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarSysUser">

#[[
  {{#  if(d.USERNAME === 'system' ){ }}
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	<a class="layui-btn layui-btn-xs" lay-event="auth">授权</a>
  {{#  } }}
]]#

</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelSysUser">
    <form id="modelSysUserForm" lay-filter="modelSysUserForm" class="layui-form model-form">
        <input name="ID" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">登录账号</label>
            <div class="layui-input-block">
                <input name="USERNAME" placeholder="请输入登录账号" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
		<div class="layui-form-item">
            <label class="layui-form-label">所属机构</label>
            <div class="layui-input-block">
				<input id="ORGID" type="text" name="ORGID"   lay-filter="ORGID" autocomplete="off" placeholder="请选择" value="" class="layui-input" lay-verify="required" required>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input name="NAME" placeholder="请输入姓名" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input name="GENDER" type="radio" value="1" title="男" />
                <input name="GENDER" type="radio" value="2" title="女"/>
				<input name="GENDER" type="radio" value="3" title="未知" checked/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证号码</label>
            <div class="layui-input-block">
                <input name="SFZHM" placeholder="请输入身份证号码" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话</label>
            <div class="layui-input-block">
                <input name="PHONE" placeholder="请输入电话" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input name="STATE" type="radio" value="0" title="正常" checked />
                <input name="STATE" type="radio" value="1" title="锁定"/>
				<input name="STATE" type="radio" value="2" title="禁用"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitSysUser" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelUserAuthScript"> 
    <div class="layui-card">
        <div class="layui-card-body">
			<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
			  <ul class="layui-tab-title">
			    <li class="layui-this">组织</li>
			    <li>角色</li>
			  </ul>
			  <div class="layui-tab-content" style="padding: 0px;">
			    <div class="layui-tab-item layui-show"> 
					<ul id="authOrgTree" class="ztree" style="height: 500px;"></ul>
					<div style="text-align: right;">
					    <button id="btnSaveOrgAuth" type="button" class="layui-btn layui-btn-normal">保存</button>
					</div>
				</div>
			    <div class="layui-tab-item">
					<table class="layui-table" id="tableUserAuthRole" lay-filter="tableUserAuthRole"></table>
					<div style="text-align: right;">
					    <button id="btnSaveRoleAuth" type="button" class="layui-btn layui-btn-normal">保存</button>
					</div>
				</div>
			  </div>
			</div>
        </div>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarAuthUserData">
#[[
  {{#  if(d.STATE === 0 ){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelData">取消</a>
  {{#  } else { }}
     <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authData">授权</a>
  {{#  } }}
]]#
</script>
<script type="text/html" id="tableBarAuthRole">

#[[
  {{#  if(d.STATE === 0 ){ }}
     <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelRole">取消</a>
  {{#  } else { }}
      <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authRole">授权</a>
  {{#  } }}
]]#
</script>
<script type="text/html" id="tableBarAuthSceneLayer">
#[[
  {{#  if(d.STATE === 0 ){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelLayer">取消</a>
  {{#  } else { }}
     <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authLayer">授权</a>
  {{#  } }}
]]#
</script>
<script type="text/html" id="tableBarAuthScenePage">
#[[
  {{#  if(d.STATE === 0 ){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelPage">取消</a>
  {{#  } else { }}
     <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authPage">授权</a>
  {{#  } }}
]]#
</script>
<!-- js部分 -->
<script>
layui.extend({
    treeSelect: '/treeSelect/treeSelect'
}).use(['layer', 'form', 'table', 'util', 'admin', 'zTree','treetable', 'config','treeSelect','element'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var treetable = layui.treetable;
        var treeSelect=layui.treeSelect;
        var config = layui.config;
        var element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块

        // 渲染表格
        var insTb = table.render({
            elem: '#tableSysUser',
            url: '#(ctx)/sys/user/listData',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'orgName', sort: false, title: '部门'},
                {field: 'NAME', sort: false, title: '姓名'},
                {
                    sort: false, templet: function (d) {
                    	if(d.GENDER=='1'){
                    		return '男';
                    	}
                    	else if(d.GENDER=='1'){
                    		return '女';
                    	}
                    	else{
                    		return "";
                    	}
                    }, title: '性别'
                },
                {field: 'PHONE', sort: false, title: '电话'},
                {
                    sort: true, templet: function (d) {
                    	if(d.LASTLOGINTIME){
                    		return util.toDateString(d.LASTLOGINTIME);
                    	}
                    	else{
                    		return "";
                    	}
                    }, title: '最后登录日期'
                },
                {field: 'LASTLOGINIP', sort: false, title: '最后登录ip'},
                {align: 'center', toolbar: '#tableBarSysUser', title: '操作', minWidth: 150}
            ]]
        });

        // 添加
        $('#btnAddSysUser').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchSysUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableSysUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            } else if (layEvent === 'auth') {  // 权限管理
                showPermModel(data.ID);
            }
        });

        // 删除
        function doDel(obj) {
        	layer.confirm('确定要删除“【<span style="color:red;">' +obj.data.NAME+ '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('sys/user/delete', {
                    id: obj.data.ID
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }

        // 显示编辑弹窗
        function showEditModel(mSysUser) {
            admin.open({
                type: 1,
                title: (mSysUser ? '修改' : '添加') + '用户',
                content: $('#modelSysUser').html(),
                area:'400px',
                success: function (layero, dIndex) {
                    var url = 'sys/user/save';
                    form.val('modelSysUserForm', mSysUser);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelSubmitSysUser)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['sysUser.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                    
                    treeSelect.render({
            	        elem: '#ORGID',
            	        data: '#(ctx)/sys/org/selectTree',
            	        type: 'get',
            	        placeholder: '请选择机构',
            	        search: true,
            	        click: function(d){
            	        },
            	        success: function (d) {
             	        	if(mSysUser&&mSysUser.ORGID){
             	        		treeSelect.checkNode('ORGID', mSysUser.ORGID); 
             	        	}
            	        }
            	    });
                }
            });
        }
        
        function showPermModel(userId) {
            admin.open({
                title: '设置权限',
                type: 1,
                content: $('#modelUserAuthScript').html(),
                area: ['500px', '660px'],
                offset: 't',
                success: function (layero, index) {
    		        $(layero).children('.layui-layer-content').css('overflow', 'hidden');
    		        
    		        //授权组织
                    var setting = {check: {enable: true}, data: {simpleData: {enable: true}}};
             		 layer.load(2);
                     admin.req('sys/user/authOrgTree', {
                         userId: userId
                     }, function (res) {
                         $.fn.zTree.init($('#authOrgTree'), setting, res);
                         layer.closeAll('loading');
                     }, 'get');
                     $('#btnSaveOrgAuth').click(function () {
                         layer.load(2);
                         var treeObj = $.fn.zTree.getZTreeObj('authOrgTree');
                         var nodes = treeObj.getCheckedNodes(true);
                         var ids = new Array();
                         for (var i = 0; i < nodes.length; i++) {
                             ids[i] = nodes[i].id;
                         }
                         
                         admin.req('sys/user/saveAuthOrg', {
                         	userId: userId,
                             authIds: JSON.stringify(ids),
                         }, function (res) {
                             layer.closeAll('loading');
                             if (200 == res.code) {
                                 layer.msg(res.message, {icon: 1});
                             } else {
                                 layer.msg(res.message, {icon: 2});
                             }
                         }, 'get'); 
                     });
                     
                     //授权角色
                     table.render({
                    	 id:'roleTable',
                         elem: '#tableUserAuthRole',
                         url: '#(ctx)/sys/user/authListRole',
                         page: true,
                         where:{
                         	userId:userId
                         },
                         height:480,
                         cellMinWidth: 100,
                         cols: [[
                             {type: 'checkbox', field: 'LAY_CHECKED', fixed: true},
                             {field: 'NAME', sort: true, title: '角色'},
                         ]],
                         done: function(res, page, count){
                        	 console.log(res);
                       	 }
                     });
                     
                     $('#btnSaveRoleAuth').click(function () {
                         layer.load(2);
                         var nodes = table.checkStatus('roleTable').data;
                         var ids = new Array();
                         for (var i = 0; i < nodes.length; i++) {
                             ids[i] = nodes[i].ID;
                         }
                         admin.req('sys/user/saveAuthRole', {
                         	 userId: userId,
                             authIds: JSON.stringify(ids),
                         }, function (res) {
                             layer.closeAll('loading');
                             if (200 == res.code) {
                                 layer.msg(res.message, {icon: 1});
                             } else {
                                 layer.msg(res.message, {icon: 2});
                             }
                         }, 'get');  
                     });
                }
            });
        }
        
    });
</script>